package com.muqi.app.user.address;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.muqi.app.network.http.ResponseUtils;

import android.content.Context;

/**
 * 
 * 城市代码
 * 
 * @author zd
 * 
 */
public class CitycodeUtil {

	private ArrayList<String> province_list = new ArrayList<String>();
	private ArrayList<String> city_list = new ArrayList<String>();
	private ArrayList<String> couny_list = new ArrayList<String>();
	public ArrayList<String> province_list_code = new ArrayList<String>();
	public ArrayList<String> city_list_code = new ArrayList<String>();
	public ArrayList<String> couny_list_code = new ArrayList<String>();
	/** 单例 */
	public static CitycodeUtil model;
	private Context context;

	private CitycodeUtil() {
	}

	public ArrayList<String> getProvince_list_code() {
		return province_list_code;
	}

	public ArrayList<String> getCity_list_code() {
		return city_list_code;
	}

	public void setCity_list_code(ArrayList<String> city_list_code) {
		this.city_list_code = city_list_code;
	}

	public ArrayList<String> getCouny_list_code() {
		return couny_list_code;
	}

	public void setCouny_list_code(ArrayList<String> couny_list_code) {
		this.couny_list_code = couny_list_code;
	}

	public void setProvince_list_code(ArrayList<String> province_list_code) {

		this.province_list_code = province_list_code;
	}

	/**
	 * 获取单例
	 * 
	 * @return
	 */
	public static CitycodeUtil getSingleton() {
		if (null == model) {
			model = new CitycodeUtil();
		}
		return model;
	}

	public ArrayList<String> getProvince(List<Cityinfo> provice) {
		if (province_list_code.size() > 0) {
			province_list_code.clear();
		}
		if (province_list.size() > 0) {
			province_list.clear();
		}
		for (int i = 0; i < provice.size(); i++) {
			province_list.add(provice.get(i).getCity_name());
			province_list_code.add(provice.get(i).getId());
		}
		return province_list;

	}

	public ArrayList<String> getCity(List<Cityinfo> provice, int provincePos) {
		if (city_list_code.size() > 0) {
			city_list_code.clear();
		}
		if (city_list.size() > 0) {
			city_list.clear();
		}
		List<Cityinfo> city = new ArrayList<Cityinfo>();
		city = provice.get(provincePos).getCityInfos();
		System.out.println("city--->" + city.toString());
		for (int i = 0; i < city.size(); i++) {
			city_list.add(city.get(i).getCity_name());
			city_list_code.add(city.get(i).getId());
		}
		return city_list;

	}

	public ArrayList<String> getCouny(List<Cityinfo> provice, int provincePos,int cityPos) {
		List<Cityinfo> couny = null;
		if (couny_list_code.size() > 0) {
			couny_list_code.clear();

		}
		if (couny_list.size() > 0) {
			couny_list.clear();
		}
		if (couny == null) {
			couny = new ArrayList<Cityinfo>();
		} else {
			couny.clear();
		}

		couny = provice.get(provincePos).getCityInfos().get(cityPos).getCityInfos();
		if(couny==null||couny.size()==0){//
			couny_list.add("");
		}
		else{
			for (int i = 0; i < couny.size(); i++) {
				couny_list.add(couny.get(i).getCity_name());
				couny_list_code.add(couny.get(i).getId());
			}
		}
		return couny_list;
	}
	
	
	public static List<Cityinfo> getCitys(Context context, String cacheJson) {
		List<Cityinfo> provinceList = null;
		JSONObject resultAllata = null;
		resultAllata = ResponseUtils.isResponseErr(context, cacheJson);
		if (resultAllata != null) {
			try {
				provinceList = new ArrayList<Cityinfo>();
				JSONArray jsonArray = resultAllata.getJSONArray("data");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject object = jsonArray.optJSONObject(i);
					Cityinfo item = null;
					item = new Cityinfo();
					item.setId(object.getString("province_id"));
					item.setCity_name(object.getString("name"));
					List<Cityinfo> cityList = new ArrayList<Cityinfo>();
					JSONArray cityArray = object.getJSONArray("cityinfo");
					for (int j = 0; j < cityArray.length(); j++) {
						JSONObject cityObject = cityArray.optJSONObject(j);
						Cityinfo cityItem = null;
						cityItem = new Cityinfo();
						cityItem.setId(cityObject.getString("city_id"));
						cityItem.setCity_name(cityObject.getString("name"));
						List<Cityinfo> districtList = new ArrayList<Cityinfo>();
						JSONArray districtArray = cityObject.getJSONArray("districtinfo");
						for (int k = 0; k < districtArray.length(); k++) {
							JSONObject districtObject = districtArray.optJSONObject(k);
							Cityinfo districtItem = null;
							districtItem = new Cityinfo();
							districtItem.setId(districtObject.getString("district_id"));
							districtItem.setCity_name(districtObject.getString("name"));
							districtList.add(districtItem);
						}
						cityItem.setCityInfos(districtList);
						cityList.add(cityItem);
					}
					item.setCityInfos(cityList);
					provinceList.add(item);
				}
			} catch (Exception e) {
				provinceList=null;
			}
		}
		return provinceList;
	}
	
}
